package com.nomi.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nomi.blog.entity.User;
import com.nomi.blog.entity.UserInfo;
import com.nomi.blog.mapper.UserInfoMapper;
import com.nomi.blog.service.UserInfoService;
import com.nomi.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 创建用户信息
     * @param userInfo
     * @return
     */
    @Override
    public Result createUserInfo(UserInfo userInfo) {
        // 判断用户信息是否存在
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userInfo.getUserId());
        UserInfo existUserInfo = userInfoMapper.selectOne(queryWrapper);
        if (existUserInfo != null){
            return Result.fail("用户信息已存在");
        }
        int result = userInfoMapper.insert(userInfo);
        if (result == 0){
            return Result.success("用户信息创建成功",userInfo);
        }else{
            return Result.fail("用户信息创建失败");
        }
    }

    /**
     * 更新用户信息
     * @param userInfo
     * @return
     */
    @Override
    @Transactional
    public Result updateUserInfo(UserInfo userInfo) {
        int result = userInfoMapper.updateById(userInfo);
        if (result > 0){
            return Result.success("用户信息更新成功");
        }else {
            return Result.fail("用户信息更新失败");
        }
    }

    /**
     * 获取用户信息
     * @param userId
     * @return
     */
    @Override
    public Result getUserInfo(Long userId) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        if (userInfo != null){
            return Result.success("获取用户信息成功", userInfo);
        }else{
            return Result.fail("用户信息不存在");
        }
    }


    /**
     * 更新用户文章数量
     * @param userId
     * @param increment
     * @return
     */
    @Override
    @Transactional
    public Result updateUserArticleCount(Long userId, int increment) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        if (userInfo != null) {
            userInfo.setArticleCount(userInfo.getArticleCount() + increment);
            int result = userInfoMapper.updateById(userInfo);
            if (result > 0) {
                return Result.success("用户文章数量更新成功");
            } else {
                return Result.fail("用户文章数量更新失败");
            }
        } else {
            return Result.fail("用户信息不存在");
        }
    }

    /**
     * 更新用户关注数
     * @param userId
     * @param increment
     * @return
     */
    @Override
    @Transactional
    public Result updateUserFollowingCount(Long userId, int increment) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        if (userInfo != null) {
            userInfo.setFollowingCount(userInfo.getFollowingCount() + increment);
            int result = userInfoMapper.updateById(userInfo);
            if (result > 0) {
                return Result.success("用户关注数更新成功");
            } else {
                return Result.fail("用户关注数更新失败");
            }
        } else {
            return Result.fail("用户信息不存在");
        }
    }

    /**
     * 更新用户粉丝数
     * @param userId
     * @param increment
     * @return
     */
    @Override
    @Transactional
    public Result updateUserFollowerCount(Long userId, int increment) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        if (userInfo != null) {
            userInfo.setFollowerCount(userInfo.getFollowerCount() + increment);
            int result = userInfoMapper.updateById(userInfo);
            if (result > 0) {
                return Result.success("用户粉丝数更新成功");
            } else {
                return Result.fail("用户粉丝数更新失败");
            }
        } else {
            return Result.fail("用户信息不存在");
        }
    }
}
